package com.kerrykidz.system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.kerrykidz.common.Constants;
import com.kerrykidz.common.PrimaryKeyGenerator;
import com.kerrykidz.membership.dao.model.GblCusCpndefEntity;
import com.kerrykidz.system.service.ICusCpndefService;
import com.kerrykidz.system.util.UserUtil;
import com.kerrykidz.system.vo.UserInfo;
import com.rdp.framework.exception.RDPException;

/**
 * 定义优惠劵控制器
 * 
 * @author huanjiang
 * 
 */
@Controller
@RequestMapping("CusCpndef")
public class CusCpndefController {

	@Autowired
	private ICusCpndefService iCusCpndefService;

	/**
	 * 跳转到添加优惠劵
	 * 
	 * @return
	 */
	@RequestMapping("toYouh")
	public ModelAndView toYouh() {
		ModelAndView mav = new ModelAndView("system/system_youh");
		return mav;
	}

	/**
	 * 优惠劵分页显示
	 * 
	 * @param gblCusCpndef
	 * @return
	 * @throws RDPException
	 */
	@RequestMapping(value = "findGblCusCpndef", method = RequestMethod.POST)
	@ResponseBody
	public GblCusCpndefEntity findGblCusCpndef() throws RDPException {
		GblCusCpndefEntity gblCusCpndef = new GblCusCpndefEntity();
		gblCusCpndef.setDelFlag("0");
		return iCusCpndefService.findGblCusCpndef(gblCusCpndef);
	}

	/**
	 * 添加新的优惠劵
	 * 
	 * @param gblCusCpndef
	 * @throws RDPException
	 */
	@RequestMapping(value = "insertGblCusCpndef", method = RequestMethod.POST)
	@ResponseBody
	public String insertGblCusCpndef(GblCusCpndefEntity gblCusCpndef) {
		try {
			UserInfo userInfo = UserUtil.getUserFromSession();
			if (userInfo != null) {
				gblCusCpndef.setPkCpndefId(PrimaryKeyGenerator
						.getRandomPrimaryKey());
				gblCusCpndef.setModiBy(userInfo.getUserId());
				iCusCpndefService.insertGblCusCpndef(gblCusCpndef);
				return Constants.SUCCESS;
			}
			return Constants.FAILURE;
		} catch (RDPException e) {
			return Constants.FAILURE;
		}
	}

	/**
	 * 修改或删除优惠劵
	 * 
	 * @param gblCusCpndef
	 * @return
	 */
	@RequestMapping(value = "editGblCusCpndef", method = RequestMethod.POST)
	@ResponseBody
	public String editGblCusCpndef(GblCusCpndefEntity gblCusCpndef) {
		try {
			UserInfo userInfo = UserUtil.getUserFromSession();
			if (userInfo != null) {
				gblCusCpndef.setModiBy(userInfo.getUserId());
				iCusCpndefService.updateGblCusCpndef(gblCusCpndef);
				return Constants.SUCCESS;
			}
			return Constants.FAILURE;
		} catch (RDPException e) {
			return Constants.FAILURE;
		}
	}
}
